package com.huaxindata.im.main.chat;

import com.huaxindata.im.main.manager.ContactManager;
import com.huaxindata.im.main.manager.HistoryContactManager;
import com.huaxindata.im.main.manager.UserManager;
import com.huaxindata.im.model.contact.Contact;
import com.huaxindata.im.model.contact.ContactGroup;
import com.huaxindata.im.model.entity.ChatMessage;
import com.huaxindata.im.model.entity.ChatMessageBody;
import com.huaxindata.im.model.entity.ChatSysMessageBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/31.
 * 聊天过程中的中间类，主要功能是起到连接会话activity的作用
 */

public class ConversationManager {
    //当前的聊天对象
    private static Contact sChatObject;
    public static Contact getChatObject() {
        return sChatObject;
    }
    public static void setChatObject(Contact chatObject) {
        sChatObject = chatObject;
    }

    public static List<OnSendMessageListener> sOnSendMessageListenerList = new ArrayList<>();
    public static OnUnSubscribeListener sUnSubscribeListener;
    //用于发送消息的回调，绑定了msgReceiverService的类来实现
    public interface OnSendMessageListener {
        void onSendMessage(ChatMessage message);
    }
    //取消订阅的接口
    public interface OnUnSubscribeListener {
        void unSubscribe(String id);
    }
    //添加发送消息回调
    public static void addOnSendMessageListener(OnSendMessageListener onSendMessageListener){
        if (!sOnSendMessageListenerList.contains(onSendMessageListener))
            sOnSendMessageListenerList.add(onSendMessageListener);
    }
    public static void removeOnSendMessageListener(OnSendMessageListener onSendMessageListener){
        sOnSendMessageListenerList.remove(onSendMessageListener);
    }
    public static void addSubscribeListener(OnUnSubscribeListener listener){
        sUnSubscribeListener =listener;
    }
    public static void sendMessage(ChatMessage message){
        for (OnSendMessageListener listener : sOnSendMessageListenerList) {
            listener.onSendMessage(message);
        }
    }
    /**
     *
     * 解除好友关系
     *@author:guokailin
     *time:2017/6/19 14:11
     */
    public static void sendDeleteFriendMessage(String respId) {
        ChatMessage message=new ChatMessage(ChatMessageBody.Type.SYSTEM,UserManager.getClientId(),respId,System.currentTimeMillis());
        ChatSysMessageBody chatSysMessageBody=new ChatSysMessageBody(ChatMessageBody.Type.SYSTEM);
        chatSysMessageBody.action=ChatSysMessageBody.SysMessageType.ACTION_RESP_FED;
        chatSysMessageBody.content=chatSysMessageBody.action+ ChatMessage.PREX + UserManager.getNickName() + ChatMessage.PREX + 2;
        message.messageBody=chatSysMessageBody;
        sendMessage(message);
        ContactManager.removeFriend(respId);//从联系人列表中删除
        HistoryContactManager.deleteHistory(respId);
    }

    /**
     *
     *向群里发送群更新的消息，通知所有群成员
     *@author:guokailin
     *time:2017/6/19 14:12
     */
    public static void sendUpdateGroupSystemMessage(ContactGroup group) {
//       HxMessage message = new HxMessage(HxMessage.Type.SYSTEM,
//       HxMessage.ACTION_UPDATE_GROUP + HxMessage.PREX + group.toString());
        ////向群中发送此消息
        ChatMessage message=new ChatMessage(ChatMessageBody.Type.SYSTEM,UserManager.getClientId(),group.getClientId(),System.currentTimeMillis());
        ChatSysMessageBody chatSysMessageBody=new ChatSysMessageBody(ChatMessageBody.Type.SYSTEM);
        chatSysMessageBody.action=ChatSysMessageBody.SysMessageType.ACTION_UPDATE_GROUP;
        chatSysMessageBody.content=chatSysMessageBody.action+ ChatMessage.PREX + group.toString();
        message.messageBody=chatSysMessageBody;
        sendMessage(message);
    }
    /**
     *
     *发送退出群消息
     *@author:guokailin
     *time:2017/6/19 14:13
     */
    public static void sendLeaveGroupSystemMessage(ContactGroup groupModel) {
//      HxMessage message = new HxMessage(HxMessage.Type.SYSTEM,
//      HxMessage.ACTION_LEAVE_GROUP + HxMessage.PREX + groupModel.groupClientId + HxMessage.PREX + UserManager.getClientId());
        ChatMessage message=new ChatMessage(ChatMessageBody.Type.SYSTEM,UserManager.getClientId(),groupModel.getClientId(),System.currentTimeMillis());
        ChatSysMessageBody chatSysMessageBody=new ChatSysMessageBody(ChatMessageBody.Type.SYSTEM);
        chatSysMessageBody.action=ChatSysMessageBody.SysMessageType.ACTION_LEAVE_GROUP;
        chatSysMessageBody.content=chatSysMessageBody.action+ ChatMessage.PREX + groupModel.groupClientId + ChatMessage.PREX + UserManager.getClientId();
        message.messageBody=chatSysMessageBody;
        sendMessage(message);
        //取消订阅该群消息
        if (sUnSubscribeListener != null) {
            sUnSubscribeListener.unSubscribe(groupModel.groupClientId);
        }
    }
}
